﻿Public Class Encrypter
    Private chrAlphabet() As Char = {"A", "B", "C", "D", "E", "F", "G", "H", _
                              "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", _
                                  "V", "W", "X", "Y", "Z", " "}

    Private chrChangingAlphabet() As Char = {"A", "B", "C", "D", "E", "F", "G", "H", _
                              "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", _
                                  "V", "W", "X", "Y", "Z", " "}

    Private Sub restoreAlphabet()
        Dim i As Integer = 0
        For Each c In chrAlphabet
            chrChangingAlphabet(i) = c
            i += 1
        Next

    End Sub

    Private Function checkPassword(ByVal Password As String) As Boolean
        Dim intIndex As Integer
        Dim intCount As Integer

        restoreAlphabet()

        ' check that Password (txtPassword.Text) is 5 characters
        If Password.Length <> 5 Then
            MsgBox("Please Enter a Password With Five Characters.")
            Return False
        End If

        Password.ToCharArray()

        ' check for duplicate characters
        If Password(0) = Password(1) Or Password(0) = Password(2) Or Password(0) = Password(3) _
            Or Password(0) = Password(4) Or Password(1) = Password(2) Or Password(1) = Password(3) _
                Or Password(1) = Password(4) Or Password(2) = Password(3) Or Password(2) = Password(4) _
                    Or Password(3) = Password(4) Then
            MsgBox("Please Do Not Use Duplicate Characters In Your password.")
            Return False
        End If

        ' create a nested for loop. the first for loop
        ' checks each individual character in the password.
        ' the second checks that the characters are letters.
        For intIndex = 0 To 4
            For intCount = 0 To 26

                ' if intSearch ever makes it to 26 (" ")
                ' return false
                If intCount = 26 Then
                    MsgBox("Please Enter a Password With Only Letters")
                    Return False
                End If

                ' if a match is found, set intSearch to 26
                ' which will end the nested for loop
                If Password(intIndex) = chrAlphabet(intCount) Then
                    intCount = 26
                End If
            Next intCount
        Next intIndex

        Return True
    End Function

    Function Encrypt(ByVal Password As String, ByVal Message As String) As String
        Password = UCase(Password)
        Message = UCase(Message)

        Dim intIndex As Integer
        Dim intCount As Integer

        ' call our functiont that checks the password
        If (checkPassword(Password) = False) Then
            Return "Invalid Password"
        End If

        Dim strEncryptedMessage As String = ""

        Dim strKey As String = Password

        For intIndex = 0 To 4
            Select Case Password(intIndex)
                Case "A"
                    intCount = 0
                Case "B"
                    intCount = 1
                Case "C"
                    intCount = 2
                Case "D"
                    intCount = 3
                Case "E"
                    intCount = 4
                Case "F"
                    intCount = 5
                Case "G"
                    intCount = 6
                Case "H"
                    intCount = 7
                Case "I"
                    intCount = 8
                Case "J"
                    intCount = 9
                Case "K"
                    intCount = 10
                Case "L"
                    intCount = 11
                Case "M"
                    intCount = 12
                Case "N"
                    intCount = 13
                Case "O"
                    intCount = 14
                Case "P"
                    intCount = 15
                Case "Q"
                    intCount = 16
                Case "R"
                    intCount = 17
                Case "S"
                    intCount = 18
                Case "T"
                    intCount = 19
                Case "U"
                    intCount = 20
                Case "V"
                    intCount = 21
                Case "W"
                    intCount = 22
                Case "X"
                    intCount = 23
                Case "Y"
                    intCount = 24
                Case "Z"
                    intCount = 25
            End Select

            ' remove character from alphabet
            chrChangingAlphabet(intCount) = " "
        Next intIndex

        ' remove spaces from alphabet
        For intIndex = 0 To 25
            If chrChangingAlphabet(intIndex) <> " " Then

                ' add remaining letters to key
                strKey += chrChangingAlphabet(intIndex)
            End If
        Next intIndex

        ' change message based on key
        For intIndex = 0 To (Message.Length - 1)
            For intCount = 0 To 25
                If Message(intIndex) <> chrAlphabet(intCount) And intCount = 25 Then
                    strEncryptedMessage += Message(intIndex)
                End If
                If Message(intIndex) = chrAlphabet(intCount) Then
                    strEncryptedMessage += strKey(intCount)
                    intCount = 25
                End If
            Next intCount
        Next intIndex

        ' return encrypted message
        Return strEncryptedMessage
    End Function

    Function Decrypt(ByVal Password As String, ByVal EncryptedMessage As String) As String
        'Convert to Uppercase
        Password = UCase(Password)
        EncryptedMessage = UCase(EncryptedMessage)

        ' create 2 counter variables
        Dim intIndex As Integer
        Dim intCount As Integer

        If (checkPassword(Password) = False) Then
            Return "Invalid Password"
        End If

        ' create variable to hold encrypted message
        Dim strDecryptedMessage As String = ""

        ' declare variable to hold key
        Dim strKey As String = Password

        ' declare counter variables

        For intIndex = 0 To 4
            Select Case Password(intIndex)
                Case "A"
                    intCount = 0
                Case "B"
                    intCount = 1
                Case "C"
                    intCount = 2
                Case "D"
                    intCount = 3
                Case "E"
                    intCount = 4
                Case "F"
                    intCount = 5
                Case "G"
                    intCount = 6
                Case "H"
                    intCount = 7
                Case "I"
                    intCount = 8
                Case "J"
                    intCount = 9
                Case "K"
                    intCount = 10
                Case "L"
                    intCount = 11
                Case "M"
                    intCount = 12
                Case "N"
                    intCount = 13
                Case "O"
                    intCount = 14
                Case "P"
                    intCount = 15
                Case "Q"
                    intCount = 16
                Case "R"
                    intCount = 17
                Case "S"
                    intCount = 18
                Case "T"
                    intCount = 19
                Case "U"
                    intCount = 20
                Case "V"
                    intCount = 21
                Case "W"
                    intCount = 22
                Case "X"
                    intCount = 23
                Case "Y"
                    intCount = 24
                Case "Z"
                    intCount = 25
            End Select

            ' remove character from alphabet
            chrChangingAlphabet(intCount) = " "
        Next intIndex

        ' remove spaces from alphabet
        For intIndex = 0 To 25
            If chrChangingAlphabet(intIndex) <> " " Then

                ' add remaining letters to key
                strKey += chrChangingAlphabet(intIndex)
            End If
        Next intIndex

        ' change message based on key
        For intIndex = 0 To (EncryptedMessage.Length - 1)
            For intCount = 0 To 25
                If EncryptedMessage(intIndex) <> strKey(intCount) And intCount = 25 Then
                    strDecryptedMessage += EncryptedMessage(intIndex)
                End If
                If EncryptedMessage(intIndex) = strKey(intCount) Then
                    strDecryptedMessage += chrAlphabet(intCount)
                    intCount = 25
                End If
            Next intCount
        Next intIndex

        ' return encrypted message
        Return strDecryptedMessage
    End Function
End Class
